package com.lee9213.conference.track.management.handler;

import com.lee9213.conference.track.management.constant.Constant;
import com.lee9213.conference.track.management.entity.SessionEntity;
import com.lee9213.conference.track.management.entity.TalkEntity;
import com.lee9213.conference.track.management.entity.TrackEntity;

import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author lee9213@163.com
 * @version 1.0
 * @date 2018-07-11 23:23
 */
public class MorningFilter implements Filter {

    @Override
    public void build(List<TalkEntity> allTalkEntities, TrackEntity trackEntity, FilterChain filterChain) {
        Iterator<TalkEntity> iterator = allTalkEntities.iterator();
        SessionEntity sessionEntity = new SessionEntity(Constant.MORNING_START_TIME, Constant.MORNING_DURATION);
        List<TalkEntity> talkEntities = new ArrayList<>();
        int minutes = 0;
        TalkEntity talkEntity;
        while (iterator.hasNext()) {
            talkEntity = iterator.next();
            if (sessionEntity.getDuration() >= talkEntity.getMinutes()) {
                talkEntity.setStartTime(LocalTime.parse(sessionEntity.getStartTime()).plusMinutes(minutes).toString());
                minutes += talkEntity.getMinutes();
                talkEntities.add(talkEntity);
                sessionEntity.setDuration(sessionEntity.getDuration() - talkEntity.getMinutes());
                iterator.remove();
            }
        }
        sessionEntity.setTalkEntity(talkEntities);
        trackEntity.addSessionEntity(sessionEntity);
        filterChain.doFilter(allTalkEntities, trackEntity);
    }
}
